<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="500" destroy-on-close @closed="$emit('closed')">
		<el-form :model="form" :rules="rules" :disabled="mode=='show'" ref="dialogForm" label-width="100px" label-position="left">
			<el-form-item label="名称" prop="meta.title">
				<el-input v-model="form.meta.title" clearable></el-input>
			</el-form-item>
			<el-form-item label="父级ID" prop="pid">
				<el-input v-model="form.pid" clearable></el-input>
			</el-form-item>
			<el-form-item label="图标" prop="meta.icon">
				<sc-icon-select v-model="form.meta.icon" :disabled="mode=='show'"></sc-icon-select>
			</el-form-item>
			<el-form-item label="展示类型" prop="meta.type">
				<el-input v-model="form.meta.type" clearable></el-input>
			</el-form-item>
			<el-form-item label="类型" prop="type">
				<el-input v-model="form.type" clearable></el-input>
			</el-form-item>
			<el-form-item label="别名" prop="name">
				<el-input v-model="form.name" clearable></el-input>
			</el-form-item>
			<el-form-item label="权限标识" prop="value">
				<el-input v-model="form.value" clearable></el-input>
			</el-form-item>
			<el-form-item label="前端地址" prop="uri">
				<el-input v-model="form.uri" clearable></el-input>
			</el-form-item>
			<el-form-item label="前端组件" prop="component">
				<el-input v-model="form.component" clearable></el-input>
			</el-form-item>
			<el-form-item label="排序" prop="sort">
				<el-input-number v-model="form.sort" controls-position="right" :min="0" style="width: 100%;"></el-input-number>
			</el-form-item>
			<el-form-item label="是否有效" prop="status">
				<el-switch v-model="form.status" :active-value="1" :inactive-value="0"></el-switch>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible=false" >取 消</el-button>
			<el-button v-if="mode!='show'" type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import scIconSelect from '@/components/scIconSelect'
	export default {
		components: {
			scIconSelect
		},
		emits: ['success', 'closed'],
		data() {
			return {
				mode: "add",
				titleMap: {
					add: '新增',
					edit: '编辑',
					show: '查看'
				},
				visible: false,
				isSaveing: false,
				//表单数据
				form: {
					id:'',
					meta: {
						title:'',
						icon:'',
						type:''
					},
					type:1,
					name:'',
					value:'',
					sort:0,
					status:1,
					uri:'',
					component:1,
					pid:0,
				},
				//验证规则
				rules: {
					/*sort: [
						{required: true, message: '请输入排序', trigger: 'change'}
					],
					name: [
						{required: true, message: '请输入名称'}
					],
					description: [
						{required: true, message: '请输入角色别名'}
					]*/
				}
			}
		},
		mounted() {

		},
		methods: {
			//显示
			open(mode='add'){
				this.mode = mode;
				this.visible = true;
				return this
			},
			//表单提交方法
			submit(){
				this.$refs.dialogForm.validate(async (valid) => {
					if (valid) {
						this.isSaveing = true;
						var res = await this.$API.system.role.add.post(this.form);
						this.isSaveing = false;
						if(res.code === 200){
							this.$emit('success', this.form, this.mode)
							this.visible = false;
							this.$message.success("操作成功")
						}else{
							this.$alert(res.message, "提示", {type: 'error'})
						}
					}
				})
			},
			//表单注入数据
			setData(data){
				/*this.form.id = data.id
				this.form.pid = data.pid
				this.form.meta = data.meta
				this.form.type = data.type
				this.form.value = data.value
				this.form.uri = data.uri
				this.form.name = data.name
				this.form.component = data.component
				this.form.sort = data.sort
				this.form.status = data.status*/
				//可以和上面一样单个注入，也可以像下面一样直接合并进去
				Object.assign(this.form, data)
			}
		}
	}
</script>

<style>
</style>
